
cc.Class({
    extends: cc.Component,

    properties: {
        CustomerPrefab:{
            default:null,
            type:cc.Prefab,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.totalTime = 4
        this.node.active =false
        this.getComponent()
        this.playAnime()

    },
    playAnime(){
        //特写动画
        this.node.active = true
        let moveIn1 = cc.moveTo(this.totalTime/2,cc.v2(0,0)).easing(cc.easeQuinticActionOut())
        let moveIn2 = cc.moveTo(this.totalTime/2,cc.v2(0,0)).easing(cc.easeQuinticActionOut())
        this.bar1.x = -this.bar1.width/2 - this.node.width/2-800
        this.bar2.x =  this.bar2.width/2 + this.node.width/2+800
        let moveOut1 = cc.moveTo(this.totalTime/2,cc.v2(this.bar1.width/2 + this.node.width /2+800 ,0)).easing(cc.easeQuinticActionIn())
        let moveOut2 = cc.moveTo(this.totalTime/2,cc.v2(-this.bar2.width/2 - this.node.width /2-800,0)).easing(cc.easeQuinticActionIn())
        this.bar1.runAction(cc.sequence(moveIn1,moveOut1))
        this.bar2.runAction(cc.sequence(moveIn2,moveOut2))
        for(let i =0;i<10;i++){
            let Customer = cc.instantiate(this.CustomerPrefab)
            Customer.x = - this.node.width/2 -200
            Customer.y = Math.random() * 600 -300
            Customer.rotation = Math.random() * 90 -45
            this.node.insertChild(Customer,0)            
            this.rolling(Customer,300)
        }
    },
    rolling(node,speed){
        let sin = Math.sin( - node.rotation / 2 / 3.1415926)
        let cos = Math.cos( - node.rotation / 2 / 3.1415926)
        let rolling = cc.sequence(cc.scaleTo(0.5,-1,1),cc.scaleTo(0.5,1,1))
        let moving = cc.moveBy(1,cc.v2(speed * cos , speed * sin))
        node.runAction(cc.repeatForever(cc.spawn(rolling,moving)))
    },
    getComponent(){
        this.bar1 = this.node.getChildByName('bar1')
        this.bar2 = this.node.getChildByName('bar2')
        this.customer = this.node.getChildByName('bar2').getChildByName('mask').getChildByName('CustomerEye')
        this.rollingCustomer = this.node.getChildByName('RollingCustomer')
        
    },

    // update (dt) {},
});
